import redis
import json
from datetime import datetime, timedelta
import random

redis_client = redis.StrictRedis(host='192.168.2.121', port=6379, db='6', password='anRvEdsYPx')
warn_data = {"@type": "com.bwda.saas.domain.workbench.card.WorkbenchCardWarn", "dayWarnCount": 39,
             "list": [{"count": 10, "hour": 0}, {"count": 0, "hour": 2}, {"count": 3, "hour": 4},
                      {"count": 4, "hour": 6}, {"count": 1, "hour": 8}, {"count": 0, "hour": 10},
                      {"count": 5, "hour": 12}, {"count": 0, "hour": 14}, {"count": 7, "hour": 16},
                      {"count": 6, "hour": 18}, {"count": 3, "hour": 20}, {"count": 0, "hour": 22}],
             "unHandleWarnCount": 22}
redis_client.set("workbench.alarm.card", json.dumps(warn_data, ensure_ascii=False))
event_data = {"@type": "com.bwda.saas.domain.workbench.card.WorkbenchCardEvent", "allEventCount": 41,
              "followEventCount": 10,
              "list": [{"count": 0, "hour": 0}, {"count": 0, "hour": 2}, {"count": 0, "hour": 4},
                       {"count": 0, "hour": 6}, {"count": 5, "hour": 8}, {"count": 0, "hour": 10},
                       {"count": 3, "hour": 12}, {"count": 0, "hour": 14}, {"count": 0, "hour": 16},
                       {"count": 0, "hour": 18}, {"count": 2, "hour": 20}, {"count": 0, "hour": 22}]}
redis_client.set("workbench.event.card", json.dumps(event_data, ensure_ascii=False))


def get_last_week_days():
    today = datetime.now()
    # 计算上周一的日期
    day_list = [today - timedelta(days=8) + timedelta(days=i) for i in range(1, 8)]

    # 转换为日期对象并格式化
    return [day.date().strftime('%Y-%m-%d') for day in day_list]


# 获取上周每一天的日期
last_week_dates = get_last_week_days()
key_list = ['警告', '次要', '重要', '紧急']
for key in key_list:
    hash_data_list = []
    for day in last_week_dates:
        hash_data_dict = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmLevelCount",
                          "count": random.randint(0, 3), "date": day, "levelCode": 0}
        hash_data_list.append(hash_data_dict)
    redis_client.hset("alarm.statistics.seven.day.level.distribution", key,
                      json.dumps(hash_data_list, ensure_ascii=False))
count_dict = {}
for day in last_week_dates:
    count_dict[day] = 0
for key in key_list:
    result = redis_client.hget("alarm.statistics.seven.day.level.distribution", key)
    data_list = json.loads(result.decode('utf-8'))
    for i in range(len(data_list)):
        count_dict[data_list[i]['date']] += data_list[i]['count']
redis_client.delete("alarm.statistics.seven.day.level.distribution.sum")
for day in last_week_dates:
    hash_data_dict = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmLevelCount", "count": count_dict[day],
                      "date": day, "levelCode": 0}
    redis_client.rpush("alarm.statistics.seven.day.level.distribution.sum",
                       json.dumps(hash_data_dict, ensure_ascii=False))
num = 0
for value in count_dict.values():
    num += value
closed = random.randint(0, num)
closed_rate = (closed / num) * 100
confirmed = random.randint(0, num - closed)
confirmed_rate = (confirmed / num) * 100
processing = num - closed - confirmed
processing_rate = (processing / num) * 100
warn_status_data = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmStatusCount", "closed": closed,
                    "closedRate": closed_rate,
                    "confirmed": confirmed, "confirmedRate": confirmed_rate, "toProcess": processing,
                    "toProcessRate": processing_rate}
redis_client.set("alarm.statistics.seven.day.status.distribution", json.dumps(warn_status_data, ensure_ascii=False))

redis_client.delete("alarm.statistics.today.level.distribution")
levelCode = 1
for key in key_list:
    hash_data_dict = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmLevelCount", "count": random.randint(0, 10),
                      "levelCode": levelCode, "levelValue": key}
    redis_client.rpush("alarm.statistics.today.level.distribution",
                       json.dumps(hash_data_dict, ensure_ascii=False))
    levelCode += 1

alarm_type_list = ['敏感数据', '异常登录', '恶意攻击', '高危操作']
for alarm_type in alarm_type_list:
    hash_data_list = []
    for day in last_week_dates:
        hash_data_dict = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmTypeCount",
                          "count": random.randint(0, 10),
                          "date": day, "typeName": alarm_type}
        hash_data_list.append(hash_data_dict)
    redis_client.hset("alarm.statistics.seven.day.type.distribution", alarm_type,
                      json.dumps(hash_data_list, ensure_ascii=False))

redis_client.delete("alarm.statistics.seven.day.type.top")
for alarm_type in alarm_type_list:
    hash_data_dict = {"@type": "com.bwda.saas.domain.alarm.dashboard.AlarmTypeCount", "count": 7,
                      "typeName": alarm_type}
    redis_client.rpush("alarm.statistics.seven.day.type.top",
                       json.dumps(hash_data_dict, ensure_ascii=False))
